package qing.web;

import static org.junit.Assert.*;

import java.awt.Color;
import java.awt.Font;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.weaver.Dump.INode;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import qing.common.des;
import qing.common.uploadImg.uploader;
//import qing.dao.MoneyLogDao;
//import qing.dao.PersonDao;
import qing.entity.Node;
import qing.entity.Person;
import qing.entity.Tpl;
import qing.entity.moneyLog;
import qing.service.NodeService;
import qing.service.PersonService;


@SuppressWarnings("unchecked")
public class testSSH {

	
	private PersonService iPerson;
	
	private NodeService inode;
//	private moneyLogService imoneyLog;
//	private TplService itpldao;
	//dao 层不可调用，所有方法均可以通过service层调用
	private ApplicationContext ctx = null;
	
	{
	ctx=new ClassPathXmlApplicationContext("applicationContext.xml");	
	iPerson=ctx.getBean(PersonService.class);
	inode=ctx.getBean(NodeService.class);
	}
	
	@Test
	public void sxtp() 
	{
        // 1-缩放图像：
        // 方法一：按比例缩放
       // uploader.scale("/jt.jpg", "jt2.jpg", 2, true);//测试OK
        // 方法二：按高度和宽度缩放
        uploader.scale2("G:\\SSPH\\src\\main\\webapp/jt.jpg", "G:\\SSPH\\src\\main\\webapp/jt2.jpg", 500, 300);//测试OK
        // 2-切割图像：
        // 方法一：按指定起点坐标和宽高切割
     //   uploader.cut("e:/abc.jpg", "e:/abc_cut.jpg", 0, 0, 400, 400 );//测试OK
        // 方法二：指定切片的行数和列数
     //   uploader.cut2("e:/abc.jpg", "e:/", 2, 2 );//测试OK
        // 方法三：指定切片的宽度和高度
     //   uploader.cut3("e:/abc.jpg", "e:/", 300, 300 );//测试OK
        // 3-图像类型转换：
   //     uploader.convert("e:/abc.jpg", "GIF", "e:/abc_convert.gif");//测试OK
        // 4-彩色转黑白：
    //    uploader.gray("e:/abc.jpg", "e:/abc_gray.jpg");//测试OK
        // 5-给图片添加文字水印：
        // 方法一：
    //    uploader.pressText("我是水印文字","e:/abc.jpg","e:/abc_pressText.jpg","宋体",Font.BOLD,Color.white,80, 0, 0, 0.5f);//测试OK
        // 方法二：
    //    uploader.pressText2("我也是水印文字", "e:/abc.jpg","e:/abc_pressText2.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.5f);//测试OK        
        // 6-给图片添加图片水印：
     //   uploader.pressImage("e:/abc2.jpg", "e:/abc.jpg","e:/abc_pressImage.jpg", 0, 0, 0.5f);//测试OK
    }
	

	
	@Test
	public void testDataSource() throws SQLException {
		javax.sql.DataSource dataSource = ctx.getBean(javax.sql.DataSource.class);
		System.out.println(dataSource.getConnection());
		
		
	}
	
	@Test
	public void testmd5() 
	{
		String str="123456";
		System.out.println("md5:"+des.md5(str));
		String aes=des.AESEncode(str);
		System.out.println("AES加密："+aes);
		System.out.println(des.AESEncode(str));
		System.out.println("AES解密："+des.AESDncode(aes));
	}
	
	
	
	@Test
	public void testaddperson(){
		Person person=new Person("whx", "123456",102f);		
		System.out.println("starting save……");
		iPerson.save(person);
		System.out.println("person:"+person);
	}
	
/*	@Test
	public void  testwithsave() {
System.out.println(imoneyLog.savemoney(1, 10f));
		
	}
	@Test
	public void  testwithdraw() {
System.out.println(imoneyLog.drawmoney(1, 900f));
		
	}*/
	
	//测试事物是否只控制在被扫描的包里面，包外为非事物控制区域(证实为非事物控制),即使使用@Transactional约束，也为非事物
	@Test	
	 public void testtransatoutpackage() {
		 Person person=iPerson.getPersonByAccount("whx");
		 Float money=10020f;		   
			
		//	imoneyLog.create(new moneyLog("测试取钱", -money, "junit test **"+new Date(), person));
			if (person.getAccount()<money) {
				throw new RuntimeException("账户余额不足，资产" + person.getAccount());
			}
			else {
				person.setAccount(person.getAccount()-money);
			}
			iPerson.update(person);
			System.out.println("over**********："+person.getAccount());
	 }
	
	
	//查询moneylog，并试图查询人员，测试懒加载(可以使用,需要开启<prop key="hibernate.enable_lazy_load_no_trans">true</prop>)
	//测试环境不能实现懒加载，正式环境可以实现
/*	@Test
	public void testfindmoneylog() {
		moneyLog log=imoneyLog.find(moneyLog.class, 1);
		System.out.println(log.getTitle());
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   System.out.println("person:"+log.getPerson());	
	}*/
	

/*	//无法测试懒加载方法，由于没法限制延长session关闭时间
	@Test
	public void  testlazyload() {
Category category=iCategory.find(Category.class, 1);
System.out.println(category.getName());
System.out.println("start lazy:");
System.out.println(category.getBoards());
	}*/
	
	
	
	@Test
	public void testgetperson() {
		Person person=iPerson.getPersonByAccount("whx");
		System.out.println(person==null?"person is null": person.toString());
	}
	@Test
	public void getpersonbyId() {
		Person person=iPerson.get("40288ad86288ecc3016288ecc65f0000");
		System.out.println(person);
	person.setName("test_ssh update");
	iPerson.update(person);
	System.out.println("update over");
	
	}
	
	@Test
	public List<Node>  digui()
	{
		List<Node> data=inode.list();
		
		List<Node> resultList = new ArrayList<>();  
        System.out.println("查询前集合大小:"+data.size());  
        long start = System.currentTimeMillis();  
        for (Node testEntity : data) {  
        	
            if (testEntity.getPid().trim().length()==0) {//父级菜单开始添加  
                resultList.add(testEntity); 
                if (ifChilds(data, testEntity.getId())) {//存在子集  
                	
                    List<Node> childs = new ArrayList<>();  
                    childs = getChildList(data,testEntity.getId(),childs);  
                    System.out.println("----->childs:"+childs);
                    resultList.addAll(childs);  
                }  
            }  
        } 
        System.out.println("resultList 的 Size:"+resultList.size());
        //打印显示
        for (Node testEntity : resultList) {  
            String s = "";  
            for (int i = 1; i < testEntity.getLayer(); i++) {  
                s = s + "\t";  
            }  
            System.out.println(s+"____"+testEntity.getTitle());  
        }  
        return resultList;
	}
	
	 //获取父id下的子集合  
    private static List<Node> getChildList(List<Node> list,String pId,List<Node> reList) {  
        for (Node testEntity : list) {  
            if (testEntity.getPid()==pId) {//查询下级菜单  
                reList.add(testEntity);  
                if (ifChilds(list, testEntity.getId())) {  
                    getChildList(list, testEntity.getId(), reList);  
                }  
            }  
        }  
        return reList;  
    }  
      
    //判断是否存在子集  
    private static boolean ifChilds(List<Node> list,String pId) {  
        boolean flag = false;  
        for (Node testEntity : list) {  
        	System.out.println("data --flag:("+(testEntity.getPid()==pId)+")---------  testEntity<"+pId+" >:"+testEntity.getPid());
        	
            if (testEntity.getPid()==pId) {  
                flag=true;  
                break;  
            }  
        }  
        return flag;  
    }  
	

	
	

}
